
package com.yimixian.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.model.CartUpdateDescription;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.util.UiUtils;
import com.yimixian.app.viewholder.CartItemViewHolder;
import com.ymx.sdk.widget.recyclerview.stickyheader.StickyHeaderAdapter;
import com.ymx.sdk.widget.recyclerview.viewholder.AbsViewHolder;
import com.ymx.sdk.widget.recyclerview.ListRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songzy on 2015/11/27.
 */
public class CartRecylerStickyAdapter extends ListRecyclerAdapter<GoodsItem> implements
        StickyHeaderAdapter<CartRecylerStickyAdapter.ViewHeadHolder> {

    LayoutInflater mInflater;
    public CartRecylerStickyAdapter(ArrayList<GoodsItem> datas, Context context) {
        super(datas, context);
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = datas;
    }

    @Override
    public int getItemLayoutResId() {
        return R.layout.cart_freebie_goods_item_view;
    }

    @Override
    public AbsViewHolder getItemViewHoler(ViewGroup parent, int viewType) {
        AbsViewHolder holder = new CartItemViewHolder(getItemView(parent, viewType), mContext);
        return holder;
    }

    @Override
    public void onBindViewHolder(AbsViewHolder holder, int position) {
//        boolean isNormalView = true;
//        int positionSize = 0;
//        int goodsGroupPosition = 0;
//        if (CartManager.getInstance().mGoodsGroup != null) {
//            for (int i = 0; i < CartManager.getInstance().mGoodsGroup.size(); i++) {
//                if (CartManager.getInstance().mGoodsGroup.get(i) != null
//                        && CartManager.getInstance().mGoodsGroup.get(i).goods != null) {
//                    if (CartManager.getInstance().mGoodsGroup.get(i).goods.size() > 0) {
//                        positionSize += CartManager.getInstance().mGoodsGroup.get(i).goods.size();
//                        if (position == 0) {
//                            isNormalView = false;
//                            goodsGroupPosition = i;
//                            break;
//                        }
//                        if (position == positionSize) {
//                            isNormalView = false;
//                            goodsGroupPosition = i + 1;
//                            break;
//                        }
//                    } else {
//                        continue;
//                    }
//                }
//            }
//        }
//        String title = "";
//        if (CartManager.getInstance().mGoodsGroup != null
//                && goodsGroupPosition < CartManager.getInstance().mGoodsGroup.size()) {
//            if (CartManager.getInstance().mGoodsGroup.get(goodsGroupPosition) != null) {
//                if (isNormalView) {
//                    title = "";
//                } else {
//                    title = CartManager.getInstance().mGoodsGroup.get(goodsGroupPosition).title;
//                }
//            }
//        }
        if (holder != null && holder instanceof CartItemViewHolder) {
            CartItemViewHolder viewHolder = (CartItemViewHolder) holder;
            //显示底部服务提醒（1.前后条配送方式id变了 2.是最后一条）
            if (position + 1 < mList.size()){//有分组但不是最后一条
                if (mList.get(position).getGroupId() != mList.get(position + 1).getGroupId()){
                    List<CartUpdateDescription> simpleDesc = mList.get(position).simpleDesc;
                    viewHolder.isNeedShowBottom(true, simpleDesc,
                            mList.get(position).isNeedBuy);
                }else{
                    viewHolder.isNeedShowBottom(false, null, mList.get(position).isNeedBuy);
                }
            }else if (position + 1 == mList.size()){//最后一条
                List<CartUpdateDescription> simpleDesc = mList.get(position).simpleDesc;
                viewHolder.isNeedShowBottom(true, simpleDesc,
                        mList.get(position).isNeedBuy);
            }else{
                viewHolder.isNeedShowBottom(false, null, mList.get(position).isNeedBuy);
            }
            viewHolder.setDatas(mList);
            viewHolder.setTitle(mList.get(position).deliverTitle);
            if (TextUtils.isEmpty(mList.get(position).deliverTitle)) {
                viewHolder.hideTitleView();
            } else{
                if (isShowHuanGou(position)){
                    viewHolder.showTitleView();
                }else
                    viewHolder.hideTitleView();
            }

            try {
                viewHolder.hideHuanGouView();
                if(!TextUtils.isEmpty(mList.get(position).giftDesc)){
                    if(position == 0 || (position > 0 && mList.get(position).getGroupId() != mList.get(position - 1).getGroupId())){
                        viewHolder.showHuanGouView(mList.get(position).giftDesc);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onBindViewHolder(holder, position);
    }

    //是否显示换购商品
    public boolean isShowHuanGou(int position){
        if (!TextUtils.isEmpty(mList.get(position).deliverTitle)){
            if (position > 0){
                if (!TextUtils.isEmpty(mList.get(position - 1).deliverTitle)){
                    if (mList.get(position - 1).getGroupId() != mList.get(position).getGroupId()){
                        return true;
                    }else return false;
                }else return true;
            }else return true;
        }else return false;
    }


    @Override
    public long getHeaderId(int position) {
        return mList.get(position).getGroupId();
    }

    @Override
    public ViewHeadHolder onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = mInflater.inflate(R.layout.cart_sort_title, parent, false);
        return new ViewHeadHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(ViewHeadHolder viewholder, final int position) {
        viewholder.tvTitle.setText(mList.get(position).groupTitle);
    }

    public class ViewHeadHolder extends RecyclerView.ViewHolder{

        public TextView tvTitle;
        public ViewHeadHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }
}
